const phoneStr=/^(13[0-9]|15[0-35-9]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
const emailStr=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
const idStr=/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
new Vue({
    el:"#m_content",
    data:{
        user:{
            loginName:'',
            userName:'',
            email:'',
            identityCode:'',
            mobile:'',
            password:'',
        },

        newUser:{
            loginName:'',
            userName:'',
            email:'',
            identityCode:'',
            mobile:'',
            password:'',
            npassword:''
        },
        infoFlag:false,
        infoText:'',
        oldPassword:'',
        login_name:''
    },
    methods:{
        getUserByLoginName()
        {
            axios.get('/api/getUserByLoginName',{
                params:{
                    loginName:this.login_name
                }
            })  .then(rs=>{
                this.user=rs.data.user
                this.oldLoginName=rs.data.user.loginName
            })
        },
        modifyUserPassword()
        {
            if(this.newUser.password==='')
            {
                this.infoFlag=true
                this.infoText="请输入密码"
                return
            }
            if(this.newUser.password.length<6)
            {
                this.infoFlag=true
                this.infoText="密码长度至少6位"
                return
            }
            if(this.newUser.npassword!==this.newUser.password)
            {
                this.infoFlag=true
                this.infoText="密码确认失败"
                return
            }
            axios.get('/api/modifyUserPassword',{
                params:{
                    oldPassword: this.oldPassword,
                    password:this.user.password
                }
            }).then(rs=>{
                if(rs.data.pass==="no")
                {
                    this.infoText="原密码输入错误"
                    this.infoFlag=true
                }else{
                    //修改密码
                    axios.post('/api/updateUserInfo',
                        this.newUser
                        ).then(rs=>{

                    })
                }
            })
        },
        updateUser(opera)
        {
            let arg=''
            switch(opera)
            {
                case 'userName':
                    arg='userName'
                    break;
                case 'mobile':
                    arg='mobile'
                    break;
                case 'email':
                    arg='email'
                    break;
                case 'identityCode':
                    arg='identityCode'
                    break;
            }
            if(arg==="userName")
            {
                if(this.newUser.userName==='')
                {
                    this.infoFlag=true
                    this.infoText="请填写用户名"
                    return
                }
            }
            if(arg==="mobile")
            {
                if(this.newUser.mobile==='')
                {
                    this.infoFlag=true
                    this.infoText="请填写手机号"
                    return
                }
                if(!phoneStr.test(this.newUser.mobile))
                {
                    this.infoFlag=true
                    this.infoText="手机号格式不正确（13 | 15 | 17 | 18 | 19开头）"
                    return
                }
            }
            if(arg==="email")
            {
                if(this.newUser.email==='')
                {
                    this.infoFlag=true
                    this.infoText="请填写邮箱"
                    return
                }
                if(!emailStr.test(this.newUser.email))
                {
                    this.infoFlag=true
                    this.infoText="邮箱格式不正确（xx@ss.ss）"
                    return
                }
            }

            if(arg==="identityCode")
            {
                if(this.newUser.identityCode==='')
                {
                    this.infoFlag=true
                    this.infoText="请填写身份证"
                    return
                }
                if(!idStr.test(this.newUser.identityCode))
                {
                    this.infoFlag=true
                    this.infoText="身份证格式不正确"
                    return
                }
            }

            axios.post('/api/updateUserInfo',
                this.newUser
            ).then(rs=>{
                    if(rs.data.isUpdate==="yes")
                    {
                        this.infoText="修改成功"
                        this.infoFlag=true
                    }
                    else{
                        this.infoText="修改失败"
                        this.infoFlag=true
                    }
                }
            )
        }
        ,
        closeInfo()
        {
            this.infoFlag=false
        }
    },
    created()
    {
        this.login_name=window.sessionStorage.getItem("loginName")
        this.newUser.loginName=this.login_name
        this.getUserByLoginName()
    }

})